#include <bits/stdc++.h>
using namespace std;
#ifndef ONLINE_JUDGE
#include "debug.hpp"
#else
#define debug(...) 8
#endif
typedef long long ll;
#define f(i, x, n) for (ll i = x; i < n; i++)
#define rf(i, x, n) for (ll i = x; i >= n; i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
const ll p = 1e9 + 7;
const ll inf = 1e18;
const ll N = 2e5 + 5;
void solve() {
ll n;
cin >> n;
vector<pair<ll,ll>> a(n);
f(i,0,n) cin >> a[i].F >> a[i].S;
ll l=0, r=n+1, ans=-1;
while(l<=r) {
ll mid = (l+r)/2;
ll cnt=0;
f(i,0,n) {
if(a[i].F>=mid-cnt-1 && cnt<=a[i].S) cnt++;
}
if(cnt >= mid) {
l=mid+1;
ans=mid;
}
else {
r=mid-1;
}
}
cout << ans << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll T = 1;
cin >> T;
while (T--)
solve();
return 0;
}
1649B - Game of Ball Passing | 572A - Arrays |
1455A - Strange Functions | 1566B - MIN-MEX Cut |
678C - Joty and Chocolate | 1352E - Special Elements |
1520E - Arranging The Sheep | 1157E - Minimum Array |
1661D - Progressions Covering | 262A - Roma and Lucky Numbers |
1634B - Fortune Telling | 1358A - Park Lighting |
253C - Text Editor | 365B - The Fibonacci Segment |
75A - Life Without Zeros | 1519A - Red and Blue Beans |
466A - Cheap Travel | 659E - New Reform |
1385B - Restore the Permutation by Merger | 706A - Beru-taxi |
686A - Free Ice Cream | 1358D - The Best Vacation |
1620B - Triangles on a Rectangle | 999C - Alphabetic Removals |
1634C - OKEA | 1368C - Even Picture |
1505F - Math | 1473A - Replacing Elements |
959A - Mahmoud and Ehab and the even-odd game | 78B - Easter Eggs |